package com.amdox.liugui.springbooteasyexcel.component;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy;
import org.apache.poi.ss.usermodel.Cell;

import java.util.List;

/**
 * 设置表头的自动调整列宽策略
 *
 * @author wb_gui_liu - 0078932 2025/06/11 12:03
 */

public class CellWidthStyleStrategy extends AbstractColumnWidthStyleStrategy {

    @Override
    protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        boolean needSetWidth = isHead || !CollUtil.isEmpty(cellDataList);
        if (needSetWidth) {
            Integer columnWidth = cell.getStringCellValue().getBytes().length;
            writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), columnWidth * 800);
        }
    }

    @Override
    protected void setColumnWidth(CellWriteHandlerContext context) {
        super.setColumnWidth(context);
    }


}
